package com.ruoyi.web.controller.lc;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lc.domain.LcDatamodelGroup;
import com.ruoyi.lc.service.ILcDatamodelGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 数据建模分组Controller
 * 
 * @author qibotian
 * @date 2025-01-07
 */
@RestController
@RequestMapping("/lc/datamodel/group")
public class LcDatamodelGroupController extends BaseController
{
    @Autowired
    private ILcDatamodelGroupService lcDatamodelGroupService;

    /**
     * 查询数据建模分组列表
     */
    @PreAuthorize("@ss.hasPermi('datamodel')")
    @GetMapping("/list")
    public AjaxResult list(LcDatamodelGroup lcDatamodelGroup)
    {
        List<LcDatamodelGroup> list = lcDatamodelGroupService.selectLcDatamodelGroupList(lcDatamodelGroup);
        return success(list);
    }

    /**
     * 导出数据建模分组列表
     */
    @PreAuthorize("@ss.hasPermi('datamodel')")
    @Log(title = "数据建模分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LcDatamodelGroup lcDatamodelGroup)
    {
        List<LcDatamodelGroup> list = lcDatamodelGroupService.selectLcDatamodelGroupList(lcDatamodelGroup);
        ExcelUtil<LcDatamodelGroup> util = new ExcelUtil<LcDatamodelGroup>(LcDatamodelGroup.class);
        util.exportExcel(response, list, "数据建模分组数据");
    }

    /**
     * 获取数据建模分组详细信息
     */
    @PreAuthorize("@ss.hasPermi('datamodel')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(lcDatamodelGroupService.selectLcDatamodelGroupById(id));
    }

    /**
     * 新增数据建模分组
     */
    @PreAuthorize("@ss.hasPermi('datamodel')")
    @Log(title = "数据建模分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LcDatamodelGroup lcDatamodelGroup)
    {
        return toAjax(lcDatamodelGroupService.insertLcDatamodelGroup(lcDatamodelGroup));
    }

    /**
     * 修改数据建模分组
     */
    @PreAuthorize("@ss.hasPermi('datamodel')")
    @Log(title = "数据建模分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LcDatamodelGroup lcDatamodelGroup)
    {
        return toAjax(lcDatamodelGroupService.updateLcDatamodelGroup(lcDatamodelGroup));
    }

    /**
     * 删除数据建模分组
     */
    @PreAuthorize("@ss.hasPermi('datamodel')")
    @Log(title = "数据建模分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{code}")
    public AjaxResult remove(@PathVariable String code)
    {
    	LcDatamodelGroup key = new LcDatamodelGroup();
    	key.setParentCode(code);
    	List<LcDatamodelGroup> groups = lcDatamodelGroupService.selectLcDatamodelGroupList(key);
    	 if (groups.size() > 0)
         {
             return error("分组下有子分组，不能删除");
         }
    	
        return toAjax(lcDatamodelGroupService.deleteLcDatamodelGroupByCode(code));
    }
}
